package com.gzhryc.shared_device.oem.code.agent;

import java.util.Date;

import org.apache.commons.lang3.time.DateUtils;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.utils.DateTools;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentReferrerBusinessConfig;
import com.gzhryc.shared_device.oem.code.profit.service.AgentReferrerOrderProfitService;

public class AgentReferrerProfitSynRunnable implements Runnable{
	
	static Logger log = Logger.getLogger(AgentReferrerProfitSynRunnable.class);
	
	private AgentReferrerBusinessConfig agentReferrerBusiCfg;
	private Date startDate;
	private Date endDate;
	private OperateInfo operateInfo;
	
	public AgentReferrerProfitSynRunnable(AgentReferrerBusinessConfig agentReferrerBusiCfg
			,Date startDate,Date endDate,OperateInfo operateInfo) {
		this.agentReferrerBusiCfg = agentReferrerBusiCfg;
		this.startDate = startDate;
		this.endDate = endDate;
		this.operateInfo = operateInfo;
	}

	@Override
	public void run() {
		int year = DateTools.differenceYear(startDate, endDate);
		
		try {
			AgentReferrerOrderProfitService.self(startDate).createToPay(agentReferrerBusiCfg, startDate, endDate, operateInfo);
		} catch (LogicException e) {
			log.error(e.getMessage(), e);
		}
	
		if(year > 0) {
			for(int i =1;i <= year;i++) {
				startDate = DateUtils.addYears(startDate, i);
				try {
					AgentReferrerOrderProfitService.self(startDate).createToPay(agentReferrerBusiCfg, startDate, endDate, operateInfo);
				} catch (LogicException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
	}

}
